1
|
|
|
import type { CssModule } from "./definitions.types"; |
2
|
|
|
import type { ClassBeming, BemInGeneral } from "./bem.types"; |
3
|
|
|
import { bem2arr } from "./bem.core"; |
4
|
|
|
import { joinWithLead, picker, wrapper } from "./core" |
5
|
|
|
import { EMPTY_OBJECT } from "./consts.json" |
6
|
|
|
|
7
|
|
|
export {setOptions} from "./bem.core" |
8
|
|
|
export { |
9
|
|
|
classBeming |
10
|
|
|
} |
11
|
|
|
|
12
|
|
|
/** Set context |
13
|
|
|
* @example |
14
|
|
|
* ```typescript |
15
|
|
|
* const bem = classBeming({classnames: require("./some.css"), className?}) |
16
|
|
|
* const bem = classBeming(this.props) |
17
|
|
|
* const bem = classBeming<Props>() |
18
|
|
|
* const bem = classBeming<MyClassNames>() |
19
|
|
|
* ``` |
20
|
|
|
*/ |
21
|
|
|
function classBeming< |
22
|
|
|
Ctx extends {classnames: Source, className?: string}, |
23
|
|
|
Source extends CssModule = Ctx["classnames"], |
24
|
|
|
//TODO #29 WithClassName extends boolean = Ctx["className"] extends string ? true : false |
25
|
|
|
>( |
26
|
|
|
context: Ctx = EMPTY_OBJECT as Ctx |
27
|
|
|
) { |
28
|
|
|
const {className} = context |
29
|
|
|
const host: ClassBeming<Source> = (arg0?, arg1?) => bem(context, arg0, arg1) |
30
|
|
|
|
31
|
|
|
return wrapper(host, className) |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
function bem< |
35
|
|
|
Source extends CssModule, |
36
|
|
|
>( |
37
|
|
|
{ |
38
|
|
|
className, |
39
|
|
|
classnames, |
40
|
|
|
}: { |
41
|
|
|
className?: string, |
42
|
|
|
classnames?: Source, |
43
|
|
|
}, |
44
|
|
|
arg0?: boolean | BemInGeneral, |
45
|
|
|
arg1?: BemInGeneral |
46
|
|
|
) { |
47
|
|
|
const source = typeof arg0 === "object" ? arg0 : arg1 |
48
|
|
|
, debemed = source && bem2arr(source) |
49
|
|
|
, picked = debemed && picker(classnames, debemed) |
50
|
|
|
, result = joinWithLead(arg0 === true && className, picked) |
51
|
|
|
|
52
|
|
|
return {className: result} |
53
|
|
|
} |
54
|
|
|
|